(************************************************************************)
(*                                                                      *)
(*    Release 3.00                                                      *)
(*                                                                      *)
(*    CoreMem - Common memory management module.                        *)
(*                                                                      *)
(*    Copyright (C) 1989, 1990 Jensen and Partners. All Rights Reserved *)
(*    Written by Simon Knight.                                          *)
(*                                                                      *)
(*    Copyright (C) 1991 Psion PLC. All Rights Reserved                 *)
(*    Modified by NSM.                                                  *)
(*                                                                      *)
(************************************************************************)

include "corelib.inc"
include "epocdefs.inc"

module CoreMem

(***************************************************************************)
section
segment _TEXT(CODE,28H)
segment _DATA(DATA,28H)

extrn _errno

select _TEXT


public __ncalloc :
public _calloc :

	push	cx
	push	dx
	mul	bx
	or	dx, dx
	je	__nmallocEntry

__nmallocError :			(* arrive here to return NULL *)
	mov	word [_errno], ENOMEM
	sub	ax, ax
	pop	dx
	pop	cx
	ret	0

public __nmalloc :
public _malloc :

	push	cx
	push	dx
	or	dl, 1

__nmallocEntry:

	or	ax, ax
	je	__nmallocError	(* return NULL if size = 0 *)
	mov	cx, ax
	mov	ah, NmHeapAllocateCell
	int	HeapManager
	jc	__nmallocError
	test	dl, dl
	jne	__noZeroFill   (* was malloc, so no zero fill *)
	push	di
	mov	di, ax
	mov	dx, ax
	sub	ax, ax
	cld
	shr	cx, 1
	rep; stosw
	pop	di
	mov	ax, dx
__noZeroFill:
	pop	dx
	pop	cx
	ret	0


(***************************************************************************)
section
segment _TEXT(CODE,28H)

select _TEXT

public _coreleft :
public __memavl :
public _nearcoreleft :

	push	bx
	mov	ah, NmHeapFreeMemory
	int	HeapManager
	pop	bx
	ret	0

(***************************************************************************)
section

segment _TEXT(CODE,28H)

select _TEXT

public __nfree :
public _free :

	push	ax
	push	bx
	mov	bx, ax
	mov	ah, NmHeapFreeCell
	int	HeapManager
	pop	bx
	pop	ax
	ret 	0

(***************************************************************************)
section
segment _TEXT(CODE,28H)

select _TEXT

public __msize :
public __nmsize :

	push	bx
	mov	bx, ax
	mov	ah, NmHeapCellSize
	int	HeapManager
	pop	bx
	ret	0

(***************************************************************************)
section
segment _TEXT(CODE,28H)

extrn __nheapchk

select _TEXT

public __heapchk :

	jmp	near __nheapchk

(***************************************************************************)
section
segment _TEXT(CODE,28H)

extrn __nheapset

select _TEXT

public __heapset :

	jmp	near __nheapset

(***************************************************************************)
section
segment _TEXT(CODE,28H)

extrn __nheapwalk

select _TEXT

public __heapwalk :

	jmp	near __nheapwalk

(***************************************************************************)
section
segment _TEXT(CODE,28H)

extrn _errno
extrn __nfree

select _TEXT

public __nrealloc :
public _realloc :

	test	bx, bx
	jne	_notFree
	push	bx
	mov	bx, ax
	call	__nfree
	pop	bx
	sub	ax, ax
	ret	0

_notFree:
	push	bx
	push	cx
	mov	cx, bx
	mov	bx, ax
	mov	ah, NmHeapReAllocateCell
	int	HeapManager
	jnc	reAllocExit
	mov	word [_errno], ENOMEM
	sub	ax, ax

reAllocExit:
	pop	cx
	pop	bx
	ret	0

end
